package com.wangjiyuan.giftshuo.ui.fragment.listtop.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.wangjiyuan.giftshuo.R;
import com.wangjiyuan.giftshuo.bean.listtopbean.singleproductbean.SingleProductBean;
import com.wangjiyuan.giftshuo.ui.fragment.listtop.helper.RecyclerViewAdapterHelper;
import com.wangjiyuan.giftshuo.bean.listtopbean.maybelikebean.MayBeLikeBean;
import com.wangjiyuan.giftshuo.ui.fragment.listtop.interfaces.MyItemListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ZangSyeriong on 2016/11/20.
 */

public class DetailsRecyclerViewAdapter extends RecyclerViewAdapterHelper<Object>{

    public static final int HEADER = 0, BODY = 1, NICK = 2;
    private MayBeLikeBean.DataBean.RecommendItemsBean recommendItemsBean;
    private List<MayBeLikeBean.DataBean.RecommendPostsBean> recommendPostsBean;
    private SingleProductBean.DataBean dataBean;

    private MyItemListener myItemListener;

    public DetailsRecyclerViewAdapter(Context context, List<Object> list, MyItemListener myItemListener) {
        super(context, list);
        this.myItemListener = myItemListener;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    //获取子布局的对应类型
    @Override
    public int getItemViewType(int position) {
        Object object = mList.get(position);
        if (object instanceof MayBeLikeBean.DataBean.RecommendItemsBean) {
            return BODY;
        }else if (object instanceof SingleProductBean.DataBean){
            return HEADER;
        }
        return NICK;
    }

    @Override
    public RecyclerView.ViewHolder onCreateMyViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case HEADER:
                view = mInflater.inflate(R.layout.details_item_recyclerview, parent, false);
                return new DetailsRecyclerViewAdapter.MyItemHeaderViewHolder(view);
            case NICK:
                view = mInflater.inflate(R.layout.details_item_recyclerview1, parent, false);
                return new DetailsRecyclerViewAdapter.MyItemNickViewHolder(view);
            case BODY:
                view = mInflater.inflate(R.layout.item_details_recyclerview, parent, false);
                return new DetailsRecyclerViewAdapter.MyItemViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindMyViewHolder(RecyclerView.ViewHolder holder, final int position) {
        int widthPixels = mContext.getResources().getDisplayMetrics().widthPixels;

        if (holder instanceof MyItemHeaderViewHolder) {
            dataBean = (SingleProductBean.DataBean) mList.get(position);

            ((MyItemHeaderViewHolder) holder).tv_details_item_title.setText(
                    dataBean.getName());
            ((MyItemHeaderViewHolder) holder).tv_details_item_price.setText(
                    "¥" + dataBean.getPrice());
            ((MyItemHeaderViewHolder) holder).tv_details_item_details.setText(
                    dataBean.getDescription());

        } else if (holder instanceof DetailsRecyclerViewAdapter.MyItemViewHolder) {

            recommendItemsBean = (MayBeLikeBean.DataBean.RecommendItemsBean) mList.get(position);
            ((MyItemViewHolder) holder).item_rv_ll_details.setLayoutParams(
                    new LinearLayout.LayoutParams(widthPixels / 2, ViewGroup.LayoutParams.WRAP_CONTENT));
            Glide.with(mContext)
                    .load(recommendItemsBean.getCoverWebpUrl())
                    .placeholder(R.mipmap.post_place_holder_image)
                    .into(((MyItemViewHolder) holder).img_rv_item_details);

            ((MyItemViewHolder) holder).tv_rv_item_title_details.setText(
                    recommendItemsBean.getName());

            ((MyItemViewHolder) holder).tv_rv_item_price_details.setText(
                    "¥" + recommendItemsBean.getPrice());

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myItemListener.getPosition(position);
                }
            });
        } else if (holder instanceof MyItemNickViewHolder) {
            recommendPostsBean = (List<MayBeLikeBean.DataBean.RecommendPostsBean>) mList.get(position);
            ((MyItemNickViewHolder) holder).getData(recommendPostsBean);
        }
    }

    static class MyItemViewHolder extends RecyclerView.ViewHolder{

        private LinearLayout item_rv_ll_details;
        private ImageView img_rv_item_details;
        private TextView tv_rv_item_title_details,
                tv_rv_item_price_details;

        public MyItemViewHolder(View itemView) {
            super(itemView);

            item_rv_ll_details = (LinearLayout) itemView.findViewById(R.id.item_rv_ll_details);
            img_rv_item_details = (ImageView) itemView.findViewById(R.id.img_rv_item_details);
            tv_rv_item_title_details = (TextView) itemView.findViewById(R.id.tv_rv_item_title_details);
            tv_rv_item_price_details = (TextView) itemView.findViewById(R.id.tv_rv_item_price_details);

        }
    }

    static class MyItemNickViewHolder extends RecyclerView.ViewHolder{

        private RecyclerView rv_item_recommend;
        private List<MayBeLikeBean.DataBean.RecommendPostsBean> objectList = new ArrayList<>();
        private DetailsRecommendRecyclerViewAdapter detailsRecommendRecyclerViewAdapter;

        public void getData(List<MayBeLikeBean.DataBean.RecommendPostsBean> recommendPostsBean) {

            objectList.clear();
            objectList.addAll(recommendPostsBean);
            detailsRecommendRecyclerViewAdapter.notifyDataSetChanged();
        }
        public MyItemNickViewHolder(View itemView) {
            super(itemView);

            rv_item_recommend = (RecyclerView) itemView.findViewById(R.id.rv_item_recommend);

            detailsRecommendRecyclerViewAdapter = new DetailsRecommendRecyclerViewAdapter(itemView.getContext(), objectList);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(itemView.getContext());
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            rv_item_recommend.setLayoutManager(linearLayoutManager);
            rv_item_recommend.setAdapter(detailsRecommendRecyclerViewAdapter);

        }
    }

    static class MyItemHeaderViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_details_item_title,
                tv_details_item_price, tv_details_item_details;

        public MyItemHeaderViewHolder(View itemView) {
            super(itemView);

            tv_details_item_title = (TextView) itemView.findViewById(R.id.tv_details_item_title);
            tv_details_item_details = (TextView) itemView.findViewById(R.id.tv_details_item_details);
            tv_details_item_price = (TextView) itemView.findViewById(R.id.tv_details_item_price);

        }
    }
}
